//
//  classifyViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "classifyViewController.h"

#define CLASSIFY_URL(a) [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/%@/n/0-10.html", a]
@interface classifyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;

@property (nonatomic, strong) NSMutableArray *videoArr;
@property (nonatomic, assign) NSInteger myIndexPathRow;

@end

@implementation classifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(returnAction)];
    [self createTableView];
}

- (void)returnAction
{
    [[avTools sharePlayer] pause];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createData
{
    [xwzAFNetTool getNetWithURL:CLASSIFY_URL(self.sid) body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *videoArr = [responseObject objectForKey:self.sid];
        self.videoArr = [videoModel baseModeWithArr:videoArr];
        if (self.videoArr) {
            [self.tableV reloadData];
        }
        
        [self.tableV.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)createTableView
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    [self.tableV.mj_header beginRefreshing];
    
    self.tableV.mj_footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    
}

- (void)getMoreData
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoModel *model = [self.videoArr objectAtIndex:indexPath.row];
    static NSString *cellStr = @"cell";
    videoTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[videoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellStr];
    }
    
    if (self.myIndexPathRow) {
        
        if (self.myIndexPathRow == indexPath.row) {
            
            [cell.contentView.layer addSublayer:cell.playerLayer];
            
        }else{
            [cell.playerLayer removeFromSuperlayer];
        }
    }
    
    [cell.imgPic sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
    cell.selectionStyle = NO;
    cell.titleLab.text = model.title;
    cell.desLab.text = model.descrip;
    
#pragma playtime
    NSInteger min = model.length / 60;
    NSInteger hour = model.length / 360;
    NSInteger remainder = model.length % 60;
    cell.timeLab.text = [NSString stringWithFormat:@"%ld%ld:%02ld", (long)hour, (long)min, remainder];
    
#pragma playCount
    if (10000 < model.playCount) {
        
        NSString *firstA = [[NSString stringWithFormat:@"%ld", (long)model.playCount] substringToIndex:1];
        NSString *firstB = [[NSString stringWithFormat:@"%ld", (long)model.playCount] substringWithRange:NSMakeRange(1, 1)];
        cell.playcountLab.text = [NSString stringWithFormat:@"%@.%@万", firstA, firstB];
        
    } else {
        
        cell.playcountLab.text = [NSString stringWithFormat:@"%ld", (long)model.playCount];
        
    }
#pragma commentCount
    if(0 == model.replyCount)
    {
        cell.wiriteLab.text = nil;
    } else {
        cell.wiriteLab.text = [NSString stringWithFormat:@"%ld", (long)model.replyCount];
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoTableViewCell *videoCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.myIndexPathRow == indexPath.row) {
        
        if ([avTools sharePlayer].rate == 0) {
            [[avTools sharePlayer] play];
        }else{
            [[avTools sharePlayer] pause];
        }
        
    }else{
        
        videoTableViewCell *oldCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.myIndexPathRow inSection:0]];
        [oldCell.playerLayer removeFromSuperlayer];
        videoModel *model = [self.videoArr objectAtIndex:indexPath.row];
        self.myIndexPathRow = indexPath.row;
        videoCell.videoM = model;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 370;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
